package com.atguigu.spzx.cart.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.common.utils.ThreadLocalUtil;
import com.atguigu.spzx.cart.service.CartService;
import com.atguigu.spzx.model.entity.h5.CartInfo;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.product.feign.ProductServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartServiceImpl implements CartService {



    @Autowired
    StringRedisTemplate  stringRedisTemplate;
    @Autowired
    ProductServiceFeign productServiceFeign;

    @Override
    public void arrToCart(Long skuId, Integer num) {

        String key = "user:cart:"+ ThreadLocalUtil.getUserInfo().getId();

        String jsonString =(String) stringRedisTemplate.opsForHash().get(key, String.valueOf(skuId));

        CartInfo cartInfo = JSON.parseObject(jsonString, CartInfo.class);

        ProductSku productId = productServiceFeign.getProductId(skuId);


        if (cartInfo == null){
            cartInfo = new CartInfo();
            cartInfo.setUserId(ThreadLocalUtil.getUserInfo().getId());
            cartInfo.setSkuId(skuId);
            cartInfo.setCartPrice(productId.getSalePrice());
            cartInfo.setImgUrl(productId.getThumbImg());
            cartInfo.setSkuNum(num);
            cartInfo.setSkuName(productId.getSkuName());
            cartInfo.setIsChecked(1);


             jsonString = JSON.toJSONString(cartInfo);

             stringRedisTemplate.opsForHash().put(key, String.valueOf(skuId), jsonString);
        }else {
            cartInfo.setSkuNum(cartInfo.getSkuNum()+num);

            jsonString = JSON.toJSONString(cartInfo);

            stringRedisTemplate.opsForHash().put(key, String.valueOf(skuId), jsonString);
        }

    }

    @Override
    public List<CartInfo> cartList() {
        Long id = ThreadLocalUtil.getUserInfo().getId();
        String key = "user:cart:"+id;
        List<Object> values = stringRedisTemplate.opsForHash().values(key);
        List<CartInfo> cartInfoList = values.stream().map(obj ->{
            String jsonString = String.valueOf(obj);
            CartInfo cartInfo = JSON.parseObject(jsonString, CartInfo.class);
            return cartInfo;
        }).collect(Collectors.toList());
        return cartInfoList;
    }

    @Override
    public void deleteCart(Long skuId) {
        Long id = ThreadLocalUtil.getUserInfo().getId();
        String Bigkey = "user:cart:"+id;
        String smallkey = String.valueOf(skuId);
        stringRedisTemplate.opsForHash().delete(Bigkey, smallkey);

    }

    @Override
    public void clearCart() {
        Long id = ThreadLocalUtil.getUserInfo().getId();
        String Bigkey = "user:cart:"+id;
        stringRedisTemplate.delete(Bigkey);
    }

    @Override
    public void checkCart(Long skuId, Integer isChecked) {
        Long id = ThreadLocalUtil.getUserInfo().getId();
        String Bigkey = "user:cart:"+id;
        String smallkey = String.valueOf(skuId);

        Object o = stringRedisTemplate.opsForHash().get(Bigkey, smallkey);
        String json = String.valueOf(o);
        CartInfo cartInfo = JSON.parseObject(json,CartInfo.class);
        cartInfo.setIsChecked(isChecked);

        stringRedisTemplate.opsForHash().put(Bigkey, smallkey, JSON.toJSONString(cartInfo));
    }

    @Override
    public void allChecked(Integer isChecked) {
        List<CartInfo> cartInfoList = this.cartList();

        Long id = ThreadLocalUtil.getUserInfo().getId();
        String Bigkey = "user:cart:"+id;
        cartInfoList.forEach(cartInfo -> {
            cartInfo.setIsChecked(isChecked);
            String smallkey = String.valueOf(cartInfo.getSkuId());

            stringRedisTemplate.opsForHash().put(Bigkey, smallkey, JSON.toJSONString(cartInfo));
        });
    }


    @Override
    public List<CartInfo> getCartInfoIsCheckedOne() {
        List<CartInfo> all = this.cartList();
        List<CartInfo> collect = all.stream().filter(cartInfo -> cartInfo.getIsChecked().intValue() == 1).collect(Collectors.toList());

        return collect;
    }

        @Override
        public void deleteCartInfoIsCheckedOne() {
            Long id = ThreadLocalUtil.getUserInfo().getId();
            String bigKey = "user:cart:" + id;

            //当前用户购物车中所有isChecked=1的cartInfo
            List<CartInfo> cartInfoList = this.getCartInfoIsCheckedOne();
            cartInfoList.forEach(cartInfo -> {
                stringRedisTemplate.opsForHash().delete(bigKey,String.valueOf(cartInfo.getSkuId()));
            });


    }
}
